package us.gaaoc.framework.webapp.backingbeans.filingSearch;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import us.gaaoc.cms.Exporter;
import us.gaaoc.cms.sustain.vo.CaseFiling;
import us.gaaoc.framework.businessmodel.Filing;
import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.Court;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.Roles;
import us.gaaoc.framework.util.CaseFilingPersistor;

import us.gaaoc.framework.util.LocalCaseInformation;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;

/**
 *  
 * Note:
 * 	This bean can't be made 'request' scope and must
 *  be 'session' scope. It must also be destroyed and re-created when
 *  the court is changed.
 * 
 * @author artripa
 * @author Michael Alexandrou August 10th, 2007
 * @author jih
 */

// This bean can't be made 
@SuppressWarnings("unchecked")
public class SearchPage extends BaseBackingBean {
	
	private Filing filing;
	private String externalCaseRecordNumber;
	private String docketId;
	private CaseRecord caseRecord;
	private String firstName;
	private String middleName;
	private String lastName;
	private Date startingDate;
	private Date endingDate;
	private boolean displayFililngsForExternalCaseRecordNumberTable = false;
	private boolean displayFilingForCivilActionNumber = false;
	private boolean displayNoFilingsMessage = false;
	private boolean displayFilingsForName = false;
	private boolean displayFilingsForPastDays;
	private boolean extendedSearch;
	private List<Filing> filingsForExternalCaseNumberList;
	private List<Filing> filingsForName;
	private List<Filing> filingsForPastDays;
	private List<Filing> filingForCivilActionNumber;
	private UIParameter parameterLinkToCaseDetails;
	private static final Logger LOG = Logger.getLogger(SearchPage.class);
	private boolean displayClearButton;
	private boolean displayUserMessage=true;
	private boolean collapsed=false;

	/** Serial Version UID*/
	private static final long serialVersionUID = 1L;
	/** A flag indicating if the search should be filtered to search only for
	 *  defendants. */
	private boolean defendantFilter;
	
	private String courtUuid;
	

	public SearchPage(){
		courtUuid = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("courtUuid");
	
		
	}

	public String retrieveFilingsForExternalCaseNumber(){
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();
		String FromPage =  (String) sessionMap.get("from");
		this.displayNoFilingsMessage = false;
		this.displayFilingForCivilActionNumber = false;
		this.displayFilingsForName = false;
		this.displayFilingsForPastDays = false;	
		
		
		if(FromPage.equalsIgnoreCase("adminChildEnforcementCaseTypePage")){
			this.displayClearButton =true;
		}
		this.filingsForExternalCaseNumberList = this.getModelFacade().findFilingsByExternalCaseRecordNumber(this.externalCaseRecordNumber, courtUuid);		
		for(int i=0;i<=this.filingsForExternalCaseNumberList.size()-1;i++){
			System.out.println("the number "+i+" element of the filings list corresponds to the case record with "+this.filingsForExternalCaseNumberList.get(i).getUuid()+" uuid");
			System.out.println("the external case number is  "+this.filingsForExternalCaseNumberList.get(i).getOCSSCaseNumber());
		}
		if(this.filingsForExternalCaseNumberList.size() == 0){
			System.out.println("There are no filings!");
			this.displayNoFilingsMessage = true;
			this.displayFililngsForExternalCaseRecordNumberTable = false;
			this.displayFilingForCivilActionNumber = false;
			this.displayFilingsForName = false;
			this.displayUserMessage=false;

			return "goToSearchPage";
		}else {
			System.out.println("There are filings!");
			this.displayFililngsForExternalCaseRecordNumberTable = true;
			this.displayNoFilingsMessage = false;
			this.displayFilingForCivilActionNumber = false;
			this.displayFilingsForName = false;
			this.displayUserMessage=false;
			return "goToSearchPage";
		}
	}
	
	public String resetFilingsForExternalCaseNumber() {
		this.setExternalCaseRecordNumber("");
		/**
		 * Author @ jogi
		 */
		this.displayFililngsForExternalCaseRecordNumberTable = false;
		this.displayNoFilingsMessage = false;
		this.displayFilingForCivilActionNumber = false;
		this.displayFilingsForName = false;
		this.displayUserMessage=true;		
		return "goToSearchPage";
	}
	
	public String retrieveFilingForCivilActionNumber(){
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();
		String FromPage =  (String) sessionMap.get("from");
		if(FromPage.equalsIgnoreCase("adminChildEnforcementCaseTypePage")){
			this.displayClearButton =true;
		}
		
		this.displayNoFilingsMessage = false;
		this.displayFilingForCivilActionNumber = false;
		this.displayFilingsForName = false;
		this.displayFilingsForPastDays = false;

		//sessionMap.put("from", "searchPage");
		System.out.println("Inside the retrieveFilingByDocketId!");
		
		this.filingForCivilActionNumber = this.getModelFacade().findFilingByCaseTrackingId(this.docketId, courtUuid);
		
	
		
		if(this.filingForCivilActionNumber == null){
			System.out.println("There is no case record!");
			this.displayNoFilingsMessage = true;
			this.displayFililngsForExternalCaseRecordNumberTable = false;
			this.displayFilingForCivilActionNumber = false;
			this.displayFilingsForName = false;
			this.displayUserMessage=false;
			return "goToSearchPage";
		}else {
			System.out.println("There are filings!");
			this.displayFilingForCivilActionNumber = true;
			this.displayFililngsForExternalCaseRecordNumberTable = false;
			this.displayNoFilingsMessage = false;			
			this.displayFilingsForName = false;
			this.displayUserMessage=false;
			return "goToSearchPage";
		}
	}
	
	public String resetFilingForCivilActionNumber() {
		this.setDocketId("");
		/**
		 * Author @ jogi
		 */
		this.displayFililngsForExternalCaseRecordNumberTable = false;
		this.displayNoFilingsMessage = false;
		this.displayFilingForCivilActionNumber = false;
		this.displayFilingsForName = false;
		this.displayUserMessage=true;		
		return "goToSearchPage";
	}
	
	/**
	 * This method loads the data from the database
	 * @return list
	 * @throws Exception
	 */
	public String retrieveFilingsForName() throws Exception {
		this.displayNoFilingsMessage = false;
		this.displayFilingForCivilActionNumber = false;
		this.displayFilingsForName = false;
		this.displayFilingsForPastDays = false;
		
		System.out.println("DEFENDANT FILTER IS SET TO " + defendantFilter);
		
			Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();
			String FromPage =  (String) sessionMap.get("from");
			if(FromPage.equalsIgnoreCase("adminChildEnforcementCaseTypePage")){
				this.displayClearButton =true;
			}
			this.filingsForName = this.getModelFacade().findCaseFilingsByPerson(this.firstName, this.middleName, this.lastName, this.defendantFilter, courtUuid);
			if(this.filingsForName.size()==0){				
				this.displayNoFilingsMessage = true;
				this.displayFililngsForExternalCaseRecordNumberTable = false;
				this.displayFilingForCivilActionNumber = false;
				this.displayFilingsForName = false;
				this.displayUserMessage=false;
				//return "goToSearchPage";				
			}else{
				this.displayFilingsForName = true;
				this.displayNoFilingsMessage = false;
				this.displayFililngsForExternalCaseRecordNumberTable = false;
				this.displayUserMessage=false;
				this.displayFilingForCivilActionNumber = false;

			}
			
		return "goToSearchPage";
	}
	
	public String resetFilingsForName() {
		this.setFirstName("");
		this.setMiddleName("");
		this.setLastName("");
		/**
		 * Author @ jogi
		 */
		this.displayFililngsForExternalCaseRecordNumberTable = false;
		this.displayNoFilingsMessage = false;
		this.displayFilingForCivilActionNumber = false;
		this.displayFilingsForName = false;
		this.displayUserMessage=true;
		
		return "goToSearchPage";
	}

	public String retrieveFilingsInitiatedPastDays() throws Exception {
		this.displayNoFilingsMessage = false;
		this.displayFilingForCivilActionNumber = false;
		this.displayFilingsForName = false;
		this.displayFilingsForPastDays = false;
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();
		//sessionMap.put("from", "searchPage");
		Person personLogin = (Person) sessionMap.get("person");
		String FromPage =  (String) sessionMap.get("from");
		if(FromPage.equalsIgnoreCase("adminChildEnforcementCaseTypePage")){
			this.displayClearButton =true;
		}

		this.filingsForPastDays = this.getModelFacade().findCaseFilingsForPastDays(this.startingDate, this.endingDate, personLogin.getUuid(), courtUuid);
		if(this.filingsForPastDays.size()==0){				
			this.displayNoFilingsMessage = true;
			this.displayFililngsForExternalCaseRecordNumberTable = false;
			this.displayFilingForCivilActionNumber = false;
			this.displayFilingsForName = false;
			this.displayUserMessage=false;
			//return "goToSearchPage";				
		}else{
			this.displayFilingsForPastDays = true;
			this.displayNoFilingsMessage = false;
			this.displayFililngsForExternalCaseRecordNumberTable = false;
			this.displayFilingForCivilActionNumber = false;
			this.displayUserMessage=false;
		}
		return "goToSearchPage";
	}
	
	public String resetFilingsInitiatedPastDays() {
		this.setStartingDate(null);
		this.setEndingDate(null);
		
		/**
		 * Author @ jogi
		 */
		this.displayFililngsForExternalCaseRecordNumberTable = false;
		this.displayNoFilingsMessage = false;
		this.displayFilingForCivilActionNumber = false;
		this.displayFilingsForName = false;
		this.displayUserMessage=true;

		return "goToSearchPage";
	}

	public String back() {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();

		String toString = "";
		String fromPage = (String) sessionMap.get("from");
		if (fromPage.equalsIgnoreCase("agentFilingsTablesPage")) {
			toString = "backByAgent";
		} else if (fromPage.equalsIgnoreCase("attFilingsTablesPage")) {
			toString = "backByAttorney";
		} else if (fromPage.equalsIgnoreCase("clerkMainPage")) {
			toString = "backByClerk";
		} else if (fromPage.equalsIgnoreCase("judgeMainPage")) {
			toString = "backByJudge";
		} else if (fromPage.equalsIgnoreCase("adminChildEnforcementCaseTypePage")) {
			toString = "goToAdminChildEnforcementCaseTypePage";
		}
		
		/**
		 * Author @ jogi
		 * This Code has been added to clear
		 * the feilds when the user navigates away from the
		 * Search page
		 */
	
		this.displayFililngsForExternalCaseRecordNumberTable = false;
		this.displayNoFilingsMessage = false;
		this.displayFilingForCivilActionNumber = false;
		this.displayFilingsForName = false;
		this.displayUserMessage=true;	

		//sessionMap.remove("displayUserMessageVarible");
		return toString;
	}



	/**
	 * This method navigates user to the Case Details Page
	 * 
	 * @return String
	 */
	public String gotoCaseDetailsPage() {
		System.out.println("Inside the gotoCaseDetails");
		LOG.info("Inside the gotoDetailsPage...");
		LOG.info("Getting the case record..");
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();					
		String toString = "";
		String fromPage = (String) sessionMap.get("from");
		
		Roles r = (Roles) sessionMap.get("role");
					
		if (r.getRole().contains("agent")){
			toString = "gotoAgentCaseDetailsPage";
		}
		if (r.getRole().contains("attorney")){
			toString = "gotoAttorneyCaseDetailsPage";
		}
		if (r.getRole().contains("clerk")){
			toString = "gotoClerkCaseDetailsPage";
		}
		if (r.getRole().contains("judge")){
			toString = "gotoJudgeCaseDetailsPage";
		}
		if (r.getRole().contains("admin")){
			toString = "gotoAdminChildEnforcementCaseDetailsPage";
		}
		
		//Filing filingForExternalCaseNumber =(Filing)this.linkToCaseDetails_forFilingForExternalCaseNumber.getValue();
		Filing filingToBeDetailed =(Filing)this.parameterLinkToCaseDetails.getValue();
		System.out.println("the filings correspond to the external case number "+filingToBeDetailed.getOCSSCaseNumber());
		if(filingToBeDetailed != null) {
			this.setFiling(filingToBeDetailed);
		}		
		//String toString = "gotoCaseDetailsPage";	 		 	
		sessionMap.put("caseUuid", filing.getUuid());
		//sessionMap.put("from", "clerkMainPage");
		sessionMap.put("from", "searchPage");
		//sessionMap.put("from", "allFilingsTablesPage");
		//sessionMap.put("from", "establishmentFilingPage"); 
		return toString;
	}

	public String clear() {
		//this.filing = (Filing) this.getLinkToCaseDetails_forFilingForExternalCaseNumber().getValue();
		this.filing = (Filing) this.parameterLinkToCaseDetails.getValue();
		this.getModelFacade().updateCaseRecordForClearCase(
				this.getFiling().getUuid(), true, courtUuid);
		// 	b7006e6c-695c-11dd-945f-00301b45ff56
		//HibernatePhaseListener.getSessionFactory().getCurrentSession().getTransaction().commit();
		//Map sessionMap = this.getFacesContext().getExternalContext()
		//.getSessionMap();
		//sessionMap.remove("caseUuid");
		return "stay";
	}
	
	 public String gotoHistoryPage() {
	               
	               System.out.println("Inside the gotoHistoryPage");
	               LOG.info("Inside the gotoHistoryPage...");
	               LOG.info("Getting the case record..");
	               Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();                                        
	               String toString = "";
	               String fromPage = (String) sessionMap.get("from");
	               if (fromPage.equalsIgnoreCase("agentFilingsTablesPage")){
	                       toString = "gotoAgentCaseHistoryPage";
	               }
	               if (fromPage.equalsIgnoreCase("attFilingsTablesPage")){
	                       toString = "gotoAttorneyCaseHistoryPage";
	               }
	               if (fromPage.equalsIgnoreCase("clerkMainPage")){
	                       toString = "gotoClerkCaseCaseHistoryPage";
	               }
	               if (fromPage.equalsIgnoreCase("judgeMainPage")){
	                       toString = "gotoJudgeCaseHistoryPage";
	               }
	               if (fromPage.equalsIgnoreCase("adminChildEnforcementCaseTypePage")){
	                       toString = "gotoAdminChildEnforcementCaseHistoryPage";
	               }
	               Filing filingToBeDetailed =(Filing)this.parameterLinkToCaseDetails.getValue();
	       		System.out.println("the filings correspond to the external case number "+filingToBeDetailed.getOCSSCaseNumber());
	       		if(filingToBeDetailed != null) {
	       			this.setFiling(filingToBeDetailed);
	       		}		
	       		//String toString = "gotoCaseDetailsPage";	 		 	
	       		sessionMap.put("caseUuid", filing.getUuid());
	               sessionMap.put("from", "searchPage");
	           return toString;
	       }
	public String getExternalCaseRecordNumber() {
		return externalCaseRecordNumber;
	}

	public void setExternalCaseRecordNumber(String externalCaseRecordNumber) {
		this.externalCaseRecordNumber = externalCaseRecordNumber;
	}

	public boolean isDisplayFililngsForExternalCaseRecordNumberTable() {
		return displayFililngsForExternalCaseRecordNumberTable;
	}

	public void setDisplayFililngsForExternalCaseRecordNumberTable(
			boolean displayFililngsForExternalCaseRecordNumberTable) {
		this.displayFililngsForExternalCaseRecordNumberTable = displayFililngsForExternalCaseRecordNumberTable;
	}

	public List<Filing> getFilingsForExternalCaseNumberList() {
		return filingsForExternalCaseNumberList;
	}

	public void setFilingsForExternalCaseNumberList(
			List<Filing> filingsForExternalCaseNumberList) {
		this.filingsForExternalCaseNumberList = filingsForExternalCaseNumberList;
	}

	/*public UIParameter getLinkToCaseDetails_forFilingForExternalCaseNumber() {
		return linkToCaseDetails_forFilingForExternalCaseNumber;
	}

	public void setLinkToCaseDetails_forFilingForExternalCaseNumber(
			UIParameter linkToCaseDetails_forFilingForExternalCaseNumber) {
		this.linkToCaseDetails_forFilingForExternalCaseNumber = linkToCaseDetails_forFilingForExternalCaseNumber;
	} */

	public Filing getFiling() {
		return filing;
	}

	public void setFiling(Filing filing) {
		this.filing = filing;
	}

	public boolean isDisplayNoFilingsMessage() {
		return displayNoFilingsMessage;
	}

	public void setDisplayNoFilingsMessage(boolean displayNoFilingsMessage) {
		this.displayNoFilingsMessage = displayNoFilingsMessage;
	}

	public String getDocketId() {
		return docketId;
	}

	public void setDocketId(String docketId) {
		this.docketId = docketId;
	}

	public CaseRecord getCaseRecord() {
		return caseRecord;
	}

	public void setCaseRecord(CaseRecord caseRecord) {
		this.caseRecord = caseRecord;
	}

	/*public boolean isDisplayCaseRecordForDocketId() {
		return displayCaseRecordForDocketId;
	}

	public void setDisplayCaseRecordForDocketId(boolean displayCaseRecordForDocketId) {
		this.displayCaseRecordForDocketId = displayCaseRecordForDocketId;
	} */

	/*public boolean isDisplayNoCaseRecordMessage() {
		return displayNoCaseRecordMessage;
	}

	public void setDisplayNoCaseRecordMessage(boolean displayNoCaseRecordMessage) {
		this.displayNoCaseRecordMessage = displayNoCaseRecordMessage;
	} */

	public UIParameter getParameterLinkToCaseDetails() {
		return parameterLinkToCaseDetails;
	}

	public void setParameterLinkToCaseDetails(UIParameter parameterLinkToCaseDetails) {
		this.parameterLinkToCaseDetails = parameterLinkToCaseDetails;
	}

	public List<Filing> getFilingForCivilActionNumber() {
		return filingForCivilActionNumber;
	}

	public void setFilingForCivilActionNumber(List<Filing> filingForCivilActionNumber) {
		this.filingForCivilActionNumber = filingForCivilActionNumber;
	}

	public boolean isDisplayFilingForCivilActionNumber() {
		return displayFilingForCivilActionNumber;
	}

	public void setDisplayFilingForCivilActionNumber(
			boolean displayFilingForCivilActionNumber) {
		this.displayFilingForCivilActionNumber = displayFilingForCivilActionNumber;
	}

	public boolean isDisplayFilingsForName() {
		return displayFilingsForName;
	}

	public void setDisplayFilingsForName(boolean displayFilingsForName) {
		this.displayFilingsForName = displayFilingsForName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public List<Filing> getFilingsForName() {
		return filingsForName;
	}

	public void setFilingsForName(List<Filing> filingsForName) {
		this.filingsForName = filingsForName;
	}

	public boolean isDisplayFilingsForPastDays() {
		return displayFilingsForPastDays;
	}

	public void setDisplayFilingsForPastDays(boolean displayFilingsForPastDays) {
		this.displayFilingsForPastDays = displayFilingsForPastDays;
	}

	public Date getEndingDate() {
		return endingDate;
	}

	public void setEndingDate(Date endingDate) {
		this.endingDate = endingDate;
	}

	public Date getStartingDate() {
		return startingDate;
	}

	public void setStartingDate(Date startingDate) {
		this.startingDate = startingDate;
	}

	public List<Filing> getFilingsForPastDays() {
		return filingsForPastDays;
	}

	public void setFilingsForPastDays(List<Filing> filingsForPastDays) {
		this.filingsForPastDays = filingsForPastDays;
	}

	public boolean isExtendedSearch() {
		return extendedSearch;
	}

	public void setExtendedSearch(boolean extendedSearch) {
		this.extendedSearch = extendedSearch;
	}

	public boolean isDisplayClearButton() {
		return displayClearButton;
	}

	public void setDisplayClearButton(boolean displayClearButton) {
		this.displayClearButton = displayClearButton;
	}
	
	/**
	 * Getter for the defendantFilter flag.
	 * 
	 * @return the flag that indicates if the search should only look for
	 * defendants.
	 */
	public boolean getDefendantFilter() {
		return defendantFilter;
	}
	
	/**
	 * Setter for the defendantFilter flag.
	 * 
	 * @param dFilter the flag that indicates if the search should only look
	 * for defendants
	 */
	public void setDefendantFilter(boolean dFilter) {
		defendantFilter = dFilter;
	}

	public boolean isDisplayUserMessage() {
		return displayUserMessage;
	}

	public void setDisplayUserMessage(boolean displayUserMessage) {
		this.displayUserMessage = displayUserMessage;
	}

	public boolean isCollapsed() {
		return collapsed;
	}

	public void setCollapsed(boolean collapsed) {
		this.collapsed = collapsed;
	}


	
}